package com.hatricks.is.json;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.ReflectUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

/**
 * @packageName: com.hatricks.json
 * @className: FastJson2EffectTest
 * @author: yanyuhui yanyh@glodon.com
 * @date: 2023/2/21 12:27
 * @description:
 */
public class FastJson2EffectTest {
    public static class Bean {
        int id = 5;

        public int getId() {
            return id;
        }
    }

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // 方法一
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Bean bean = ReflectUtil.newInstance(Bean.class);
        for (int i = 0; i < 10000; i++) {
            Method methodGetId = Bean.class.getMethod("getId");
            int value1 = (Integer) methodGetId.invoke(bean);
        }
        stopWatch.stop();

        stopWatch.start();
        for (int i = 0; i < 10000; i++) {
            // 方法二
            java.util.function.ToIntFunction<Bean> function = Bean::getId;
            int value2 = function.applyAsInt(bean);
        }
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint(TimeUnit.MILLISECONDS));
    }

    //public static String getNewJson() {
    //    java.util.function.ToIntFunction<Bean> function = LambdaMetafactory.metafactory(
    //            Bean.class.,
    //            "applyAsInt",
    //            methodHanlder,
    //            methodType(ToIntFunction.class),
    //            lookup.findVirtual(int.class, "getId", methodType(int.class)),
    //            methodType(int.class)
    //    );
    //    int i = function.applyAsInt(bean);
    //    return String.valueOf(2);
    //}
    //function


}
